<?php
Class Common extends CI_Controller{
	function upload_image(){
		try{
			$config['upload_path'] = './assets/temp/image/';
	        $config['allowed_types'] = 'jpeg|png|jpg|flv|mp3|wav';
	        $config['max_size'] = '10000';
	        $config['overwrite']= true;
	        
	        #up image
			$this->load->library('upload', $config);
			//upload main image
	        if(!$this->upload->do_upload('picture')){
	            $e = $this->upload->display_errors();
	           // print_r($e);
	        }
			//$this->upload->do_upload('picture');
			$data = $this->upload->data();
			$small_image = "small_".$data['file_name'] ;
			#resize image
			$w_small = 190;
        	$h_small = 220;
        	
        	$w_big = 400;
        	$h_big = 650;
			
        	$this->load->library('image_lib');
	        $config['image_library'] = 'gd2';
	        $config['source_image'] = $data['full_path'];
	        //$config['create_thumb'] = TRUE;
	        $config['new_image'] =$small_image; 
	        $config['maintain_ratio'] = TRUE;
	        $config['thumb_marker'] = '';
	        $config['width'] = $w_small;
	        $config['height'] = $h_small;
                
        	$this->image_lib->initialize($config);
        	
        	//resize hinh nho
        	if($data['image_width']<$w_small && $data['image_height']<$h_small){
	        }else{
	            $this->image_lib->resize();
	        }
	        
	        //resize hinh lon
			if($data['image_width']<$w_big && $data['image_height']<$h_big){
	        }else{
	        	
	        	$config['new_image'] =$data['file_name']; 
	        	$config['width'] = $w_big;
	        	$config['height'] = $h_big;
	        	$this->image_lib->initialize($config);
	            $this->image_lib->resize();
	        }
	        	
	        // $file = $data['file_name'];
	        echo json_encode(array( "error"=>false,"msg"=>"success","path_file"=>"/assets/temp/image/".$small_image,"path_file_full"=>base_url("/assets/temp/image/".$small_image),"dir"=>"image"));
	        
	        #link tham khao : http://stackoverflow.com/questions/9615147/upload-image-in-codeigniter-using-ajax
	        
		}
		catch(Exception $e){
			echo json_encode(array("error"=>true,"msg"=>$e->getMessage()));
		}		
	}
} 
?>